<template>
<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="left" label-width="100px">
    <el-form-item label="餐标" prop="price" required>
        <el-input type="number" v-model="ruleForm.price" placeholder="" @input="setTotal"></el-input>
    </el-form-item>
    <el-form-item label="" prop="amount" required>
        <el-input type="number" v-model="ruleForm.amount" placeholder="" @input="setTotal"></el-input>
    </el-form-item>
    <el-form-item label="优惠金额" prop="discount">
        <el-input type="number" v-model="ruleForm.discount" placeholder="" @input="setTotal"></el-input>
    </el-form-item>
    <el-form-item label="金额小计" prop="total" required>
        <el-input type="number" v-model="ruleForm.total" placeholder="" readonly></el-input>
    </el-form-item>
</el-form>
</template>

<script>
const ruleForm = {
    price: '',
    amount: '',
    discount: '',
    total: '',
};
export default {
    data() {
        return {
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                price: [{
                    required: true,
                    message: "请录入餐标",
                }],
                amount: [{
                    required: true,
                    message: "请录入",
                }],
                total: [{
                    required: true,
                    message: "小计不能为空",
                }],
            },
        }
    },
    methods: {
        setTotal() {
            let tablePrice = parseFloat(this.ruleForm.price || 0);
            let tableAmount = parseFloat(this.ruleForm.amount || 0);
            let discount = parseFloat(this.ruleForm.discount || 0);
            let total = (tablePrice * tableAmount - discount).toFixed(2);
            this.ruleForm.total = total;
        },
        async submit() {
            await this.$refs['ruleForm']
                .validate(valid => {
                    return valid;
                })
        }
    }
}
</script>

<style>

</style>
